﻿

using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


namespace WebSite.admin.order
{
    public partial class meterialOrderAdd : BaseAdmin
    {

        public StringBuilder SelectSubCompnay = new StringBuilder();
        protected string kw = "";
        protected string order = "id";

        protected int pageCount = 0;
        protected int pageIndex = 1;
        protected int pageSize = 20;
        protected PageList pl = new PageList();
        protected int t = 0;
        protected int total = 0;

 
        string _agentTypeValue1 = string.Empty;
        string _agentTypeValue0 = "checked=\"checked\"";

        public string AgentTypeValue1
        {
            get {
                
                return _agentTypeValue1; }
        
        }
        public string AgentTypeValue0
        {
            get
            {

                return _agentTypeValue0;
            }

        }

 
        protected string GetCategoryName(object category)
        {
            string str = "";
            try
            {
                ArrayList list = ArrayList.Adapter(category.ToString().Split(new char[] { ',' }));
                for (int i = 0; i < list.Count; i++)
                {
                    DataTable categoryByCategoryIdentity = BaseFactory.theCategory.GetCategoryByCategoryIdentity(Convert.ToInt32(list[i]));
                    str = str + ((i == 0) ? categoryByCategoryIdentity.Rows[0]["categoryName"].ToString() : ("," + categoryByCategoryIdentity.Rows[0]["categoryName"].ToString()));
                }
            }
            catch
            {
            }
            return str;
        }

        private void GoodsBind()
        {
            try
            {
                if (base.Request.QueryString["p"] != null)
                {
                    this.pageIndex = int.Parse(base.Request.QueryString["p"]);
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    this.pageSize = Convert.ToInt32(base.Request.QueryString["ps"]);
                }
                if (base.Request.QueryString["or"] != null)
                {
                    this.order = base.Request.QueryString["or"];
                }

                if (base.Request.QueryString["agentType"] != null)
                {
                    if (base.Request.QueryString["agentType"] == "1")
                    {
                        this._agentTypeValue0 = "checked=\"checked\"";
                        this._agentTypeValue1 = "";
                    }
                    else
                    {
                        this._agentTypeValue1 = "checked=\"checked\"";
                        this._agentTypeValue0 = "";
                    }
                }
                this.pl.TblName = "view_goods";
                this.pl.StrGetFields = "id,goodsName,goodsNo,category,companyId, salePrice,isShelves,isBest,isNew,isTopSelling,inventory,isFromFreight,isnull(isProfShelves,0) as isProfShelves,realyCount ";
                this.pl.FldName = this.order;
                this.pl.PageSize = this.pageSize;
                this.pl.PageIndex = this.pageIndex;
                this.pl.DoCount = 1;
                StringBuilder sbWhere = new StringBuilder( " isnull(isShelves,0) = 1 ");
                string sOrderCompanyId = string.Empty;
                if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
                {
                    sbWhere.Append(" and companyId = " + base.Administrator.CompanyId);
                    sOrderCompanyId = Request.QueryString["cid"];
                }
                else
                {
                    sOrderCompanyId = base.Administrator.CompanyId;
                    tbCompany oCompany = BaseFactory.theCompany.GetModel(int.Parse(base.Administrator.CompanyId));
                    if (oCompany != null)
                    {
                        sbWhere.Append( " and companyId = " + oCompany.ParentId);
                    }

                }
        
                if ((base.Request.QueryString["t"] != null) && !string.IsNullOrEmpty(base.Request.QueryString["kw"]))
                {
                    this.kw = base.Request.QueryString["kw"];
                    if (base.Request.QueryString["t"] != "categoryName")
                    {
                        sbWhere.Append( " and " + base.Request.QueryString["t"] + " like '%" + this.kw + "%'");
                    }
                    else
                    {
                        sbWhere.Append(  " and id in(select goods from tbGoodsCategory a left join tbCategory b on a.category=b.categoryIdentity where b.categoryName like '%" + this.kw + "%')");
                    }
                }
                this.pl.StrWhere = sbWhere.ToString();
                this.total = Convert.ToInt32(BaseFactory.thePageList.GetPageList(this.pl).Rows[0]["Total"]);
                this.pl.DoCount = 0;
                DataTable pageList = BaseFactory.thePageList.GetPageList(this.pl);
                new PageListNo().DelPageSize(pageList, this.total);
                if (pageList.Rows.Count == 0)
                {
                    this.btnNext.Visible = false;
                }
                decimal dOnSale = BaseFactory.theCompany.GetCompanyOnSale(sOrderCompanyId);
                base.TempOrderOnSale = dOnSale;

                pageList.Columns.Add(new DataColumn("onSale", Type.GetType("System.String")));
                pageList.Columns.Add(new DataColumn("realyPrice", Type.GetType("System.String")));

                foreach (DataRow dr in pageList.Rows)
                {
                    dr["onSale"] = dOnSale.ToString("n");
                    dr["realyPrice"] = (decimal.Parse(dr["salePrice"].ToString()) * dOnSale).ToString("n");
                }
                this.rptGoods.DataSource = pageList;
                this.rptGoods.DataBind();
            }
            catch
            {
            }
        }

        void BindCompanyList()
        {
            DataSet dsCompany = BaseFactory.theCompany.GetList(" parentId= " + base.Administrator.CompanyId);
            if (dsCompany != null && dsCompany.Tables.Count > 0)
            {
                foreach (DataRow dr in dsCompany.Tables[0].Rows)
                {
                    
                        this.SelectSubCompnay.AppendFormat("  <option value=\"{1}\">{0}</option>"
                                                       , dr["name"].ToString()
                                                       , dr["id"].ToString()
                                                       );
     

                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
          
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.OrderMateialAdd);
                this.DeletedGoods();
                this.BindCompanyList();
                if (base.Request.QueryString["t"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sGs", "<script type=\"text/javascript\">selectIsExitItem(\"selectGoods\",\"" + base.Request.QueryString["t"] + "\");</script>");
                }
                if (base.Request.QueryString["ps"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sPs", "<script type=\"text/javascript\">selectIsExitItem(\"selectPageSize\",\"" + base.Request.QueryString["ps"] + "\");</script>");
                }
                if (base.Request.QueryString["cid"] != null)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "cid", "<script type=\"text/javascript\">selectIsExitItem(\"selectSubCompany\",\"" + base.Request.QueryString["cid"] + "\");</script>");
                }
                this.btnNext.Attributes.Add("disabled", "disabled");
                this.GoodsBind();
            }
        }


        void DeletedGoods()
        {

            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            int goods = 0;
            if (Request.QueryString["action"] == "del")
            {
                goods = Convert.ToInt32(Request.QueryString["id"]);
                DataTable goodGalleryByGoods = BaseFactory.theGoodGallery.GetGoodGalleryByGoods(goods);
                if (BaseFactory.theGoods.Delete(goods))
                {
                    foreach (DataRow row in goodGalleryByGoods.Rows)
                    {
                        if ((row["thumbImg"].ToString() != "") && File.Exists(base.Server.MapPath(row["thumbImg"].ToString())))
                        {
                            File.Delete(base.Server.MapPath(row["thumbImg"].ToString()));
                        }
                        if ((row["smallImg"].ToString() != "") && File.Exists(base.Server.MapPath(row["smallImg"].ToString())))
                        {
                            File.Delete(base.Server.MapPath(row["smallImg"].ToString()));
                        }
                        if ((row["bigImg"].ToString() != "") && File.Exists(base.Server.MapPath(row["bigImg"].ToString())))
                        {
                            File.Delete(base.Server.MapPath(row["bigImg"].ToString()));
                        }
                    }
                    base.Response.Redirect(pathAndQuery.Replace("action=","noaction="));
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sd", "<script language=\"javascript\" defer>alert('删除失败！');</script>");
                }
            }
        }



        protected void btnNext_Click(object sender, EventArgs e)
        {
            base.Administrator.MeteriaOrderCardIds = base.Request.Form["Item"];
            this.Page.Server.Transfer("meterialOrderAdd02.aspx");
        }

    }
}

